package com.loong.musicdemo.util.httpUtil;

import java.io.IOException;

import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

/**
 * Http请求类
 */
public class HttpUtil {
    /**
     * 异步请求
     * @param address 网络地址
     * @param callback 回调
     */
    public static void sendOKHttpRequest(String address, okhttp3.Callback callback){
        OkHttpClient client = new OkHttpClient();
        Request request = new Request.Builder().url(address).build();
        client.newCall(request).enqueue(callback);
    }

    /**
     * 同步请求
     * @param address 地址
     * @return 响应内容
     */
    public static String sendReturnOKHttpRequest(String address){
        OkHttpClient client = new OkHttpClient();
        Request request = new Request.Builder().url(address).build();
        try {
            Response response = client.newCall(request).execute();
            return response.body().string();//获取Http响应报文的结果
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
}
